package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.AppGradeRecord;
import com.ruoyi.background.domain.AppWechatUser;
import com.ruoyi.background.domain.AppWorkTeam;
import com.ruoyi.background.domain.AppWorkTeamUser;
import com.ruoyi.background.mapper.AppWorkTeamMapper;
import com.ruoyi.background.mapper.AppWorkTeamUserMapper;
import com.ruoyi.background.service.IAppGradeRecordService;
import com.ruoyi.background.service.IAppWechatUserService;
import com.ruoyi.background.service.IAppWorkTeamService;
import com.ruoyi.background.service.IAppWorkTeamUserService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Random;

/**
 * 健步走队伍Service业务层处理
 *
 * @author susu
 * @date 2024-09-04
 */
@Service
public class AppWorkTeamServiceImpl implements IAppWorkTeamService {
    @Autowired
    private AppWorkTeamMapper appWorkTeamMapper;


    @Autowired
    private AppWorkTeamUserMapper appWorkTeamUserMapper;

    @Autowired
    private IAppWorkTeamUserService teamUserService;

    @Autowired
    private IAppWechatUserService wechatUserService;

    @Autowired
    private IAppGradeRecordService gradeRecordService;

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询健步走队伍
     *
     * @param id 健步走队伍主键
     * @return 健步走队伍
     */
    @Override
    public AppWorkTeam selectAppWorkTeamById(Long id) {
        return appWorkTeamMapper.selectAppWorkTeamById(id);
    }

    /**
     * 获取用户的队伍信息
     *
     * @return 结果
     */
    @Override
    public AppWorkTeamUser getUserTeamInfo() {
        return appWorkTeamUserMapper.selectTeamInfoByUserId(SecurityUtils.getUserId());
    }

    /**
     * 查询健步走队伍列表
     *
     * @param appWorkTeam 健步走队伍
     * @return 健步走队伍
     */
    @Override
    public List<AppWorkTeam> selectAppWorkTeamList(AppWorkTeam appWorkTeam) {
        return appWorkTeamMapper.selectAppWorkTeamList(appWorkTeam);
    }

    /**
     * 新增健步走队伍
     *
     * @param appWorkTeam 健步走队伍
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAppWorkTeam(AppWorkTeam appWorkTeam) {

        //判断是否加入或者创建队伍
        AppWorkTeamUser user1 = teamUserService.selectAppWorkTeamUserByUserId(SecurityUtils.getUserId());
        if (!ObjectUtils.isEmpty(user1))
            throw new RuntimeException("您已创建或者加入队伍，每人最多处于一支队伍！");

        appWorkTeam.setCreateTime(DateUtils.getNowDate());
        appWorkTeam.setUserId(SecurityUtils.getUserId());
        appWorkTeam.setDeptId(SecurityUtils.getDeptId());
        appWorkTeam.setPeopleSum(1);
        appWorkTeam.setCreateBy(SecurityUtils.getUsername());
        //生成code
        AppWorkTeam code;
        StringBuilder teamCode = new StringBuilder();
        do {
            Random ran = new Random();
            for (int i = 0; i < 6; i++) {
                int s = ran.nextInt(10);
                teamCode.append(s);
            }
            code = appWorkTeamMapper.selectAppWorkTeamByCode(teamCode.toString());
        } while (!ObjectUtils.isEmpty(code));
        appWorkTeam.setTeamCode(teamCode.toString());
        int res = appWorkTeamMapper.insertAppWorkTeam(appWorkTeam);
        AppWorkTeamUser user = new AppWorkTeamUser();
        user.setTeamId(appWorkTeam.getId());
        user.setTeamName(appWorkTeam.getName());
        user.setUserId(SecurityUtils.getUserId());
        user.setDeptId(SecurityUtils.getDeptId());
        user.setCreateTime(DateUtils.getNowDate());
        teamUserService.insertAppWorkTeamUser(user);
        //发放积分
        int grade = Integer.parseInt(sysConfigService.selectConfigByKey("app.work.team.grade"));
        AppWechatUser wechatUser = wechatUserService.selectAppWechatByUserId(SecurityUtils.getUserId());
        AppGradeRecord record = new AppGradeRecord();
        record.setUserId(SecurityUtils.getUserId());
        record.setGrade(grade);
        record.setType(0);
        record.setBeforeGrade(wechatUser.getGrade());
        record.setCreateTime(DateUtils.getNowDate());
        record.setRemark("创建云上约步队伍奖励");
        gradeRecordService.insertAppGradeRecord(record);
        AppWechatUser update = new AppWechatUser();
        update.setId(wechatUser.getId());
        update.setGrade(wechatUser.getGrade() + grade);
        wechatUserService.updateAppWechatUser(update);
        return res;
    }

    /**
     * 用户加入队伍
     *
     * @param code 队伍编码
     * @return 结果
     */
    @Override
    @Transactional
    public int userJoinTeam(String code) {
        AppWorkTeam team = appWorkTeamMapper.selectAppWorkTeamByCode(code);
        if (ObjectUtils.isEmpty(team))
            throw new RuntimeException("无编号为：" + code + " 的队伍！");
        int max = Integer.parseInt(sysConfigService.selectConfigByKey("app.team.max.people"));
        if (team.getPeopleSum() >= max)
            throw new RuntimeException("队伍已满员！");
        AppWorkTeamUser user = new AppWorkTeamUser();
        user.setTeamId(team.getId());
        user.setTeamName(team.getName());
        user.setUserId(SecurityUtils.getUserId());
        user.setDeptId(SecurityUtils.getDeptId());
        user.setCreateTime(DateUtils.getNowDate());
        int i = teamUserService.insertAppWorkTeamUser(user);
        //发放积分
        int grade = Integer.parseInt(sysConfigService.selectConfigByKey("app.work.team.grade"));
        AppWechatUser wechatUser = wechatUserService.selectAppWechatByUserId(SecurityUtils.getUserId());
        AppGradeRecord record = new AppGradeRecord();
        record.setUserId(SecurityUtils.getUserId());
        record.setGrade(grade);
        record.setType(0);
        record.setBeforeGrade(wechatUser.getGrade());
        record.setCreateTime(DateUtils.getNowDate());
        record.setRemark("加入云上约步队伍奖励");
        gradeRecordService.insertAppGradeRecord(record);
        AppWechatUser update = new AppWechatUser();
        update.setId(wechatUser.getId());
        update.setGrade(wechatUser.getGrade() + grade);
        wechatUserService.updateAppWechatUser(update);
        AppWorkTeam updateTeam = new AppWorkTeam();
        updateTeam.setId(team.getId());
        updateTeam.setPeopleSum(team.getPeopleSum() + 1);
        appWorkTeamMapper.updateAppWorkTeam(updateTeam);
        return i;
    }

    /**
     * 修改健步走队伍
     *
     * @param appWorkTeam 健步走队伍
     * @return 结果
     */
    @Override
    public int updateAppWorkTeam(AppWorkTeam appWorkTeam) {
        return appWorkTeamMapper.updateAppWorkTeam(appWorkTeam);
    }

    /**
     * 批量删除健步走队伍
     *
     * @param ids 需要删除的健步走队伍主键
     * @return 结果
     */
    @Override
    public int deleteAppWorkTeamByIds(Long[] ids) {
        return appWorkTeamMapper.deleteAppWorkTeamByIds(ids);
    }

    /**
     * 删除健步走队伍信息
     *
     * @param id 健步走队伍主键
     * @return 结果
     */
    @Override
    public int deleteAppWorkTeamById(Long id) {
        return appWorkTeamMapper.deleteAppWorkTeamById(id);
    }
}
